package han.chensing.latos.maker.windows.menus.popup;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import han.chensing.latos.maker.res.Res;

public class RightClickMenu extends Table {

    final float width=150;
    final float heightPerItem=30;

    CloseListener closeListener;

    public RightClickMenu() {
        super(Res.get().get("skinBasic", Skin.class));
        setWidth(width);
    }

    public void setCloseListener(CloseListener closeListener) {
        this.closeListener = closeListener;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        if (getZIndex() != getParent().getChildren().size - 1) {
            if (closeListener!=null) closeListener.onClose(true);
            remove();
        }
    }

    public static RightClickMenu getNewRightClickMenu(float x,float y,RightClickMenuList list){
        if (list==null)return null;
        List<RightClickMenuItem> entries = list.getEntries();
        RightClickMenuItem[] rightClickMenuItems=new RightClickMenuItem[entries.size()];
        entries.toArray(rightClickMenuItems);
        boolean[] enables=new boolean[entries.size()];
        for (int i = 0; i < rightClickMenuItems.length; i++) {
            enables[i]=rightClickMenuItems[i].enabled;
        }
        return getNewRightClickMenu(x,y,enables,rightClickMenuItems);
    }

    public static RightClickMenu getNewRightClickMenu(float x, float y,boolean[] enabled, RightClickMenuItem... rightClickMenuItems){
        if (rightClickMenuItems==null) return null;
        RightClickMenu rightClickMenu = new RightClickMenu();
        float height = rightClickMenuItems.length * rightClickMenu.heightPerItem;
        rightClickMenu.setX(x);
        rightClickMenu.setY(y-height);
        rightClickMenu.setHeight(height);
        Skin skinBasic = Res.get().get("skinBasic", Skin.class);
        if (enabled==null||enabled.length!=rightClickMenuItems.length){
            enabled=new boolean[rightClickMenuItems.length];
            Arrays.fill(enabled,true);
        }
        for (int i = 0; i < rightClickMenuItems.length; i++) {
            RightClickMenuItem rightClickMenuItem = rightClickMenuItems[i];
            TextButton textButton=new TextButton(rightClickMenuItem.text, skinBasic);
            textButton.addListener(new ClickListener(){
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    super.clicked(event, x, y);
                    rightClickMenuItem.listener.run();
                    rightClickMenu.remove();
                    if (rightClickMenu.closeListener!=null) rightClickMenu.closeListener.onClose(false);
                }
            });
            textButton.setDisabled(!enabled[i]);
            rightClickMenu.add(textButton).width(rightClickMenu.width)
                    .height(rightClickMenu.heightPerItem)
                    .row();
        }
        return rightClickMenu;
    }

    public static class RightClickMenuItem{
        boolean enabled=true;
        Runnable listener;
        String text;
        public RightClickMenuItem(String text,Runnable listener) {
            this.listener = listener;
            this.text = text;
        }
        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
    }

    public static class RightClickMenuList{
        protected List<RightClickMenuItem> entries;
        public RightClickMenuList(){
            this.entries=new ArrayList<>();
        }

        public List<RightClickMenuItem> getEntries() {
            return entries;
        }
    }

    public interface CloseListener{
        void onClose(boolean lostFocus);
    }
}
